package com.qdc.datacenters.controller;

import com.github.pagehelper.PageInfo;
import com.qdc.datacenters.annotation.Log;
import com.qdc.datacenters.domain.ResponseResult;
import com.qdc.datacenters.domain.query.sensitive.SensitiveAddQuery;
import com.qdc.datacenters.domain.query.sensitive.SensitiveQuery;
import com.qdc.datacenters.domain.query.sensitive.SensitiveRecordQuery;
import com.qdc.datacenters.domain.vo.sensitive.SensitiveGroupRecordVo;
import com.qdc.datacenters.domain.vo.sensitive.SensitiveSingleRecordVo;
import com.qdc.datacenters.domain.vo.sensitive.SensitiveVo;
import com.qdc.datacenters.enums.BusinessTypeEnum;
import com.qdc.datacenters.service.IChatContactMsgSensitiveService;
import com.qdc.datacenters.service.ISensitiveWordsResultService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author danmo
 * @date 2024-03-07 16:54
 **/

@Tag(name = "敏感词管理")
@RestController
@RequestMapping("/sensitive")
public class SensitiveController extends BaseController{

    @Autowired
    private IChatContactMsgSensitiveService sensitiveService;

    @Autowired
    private ISensitiveWordsResultService iSensitiveWordsResultService;


    @Log(title = "新增敏感词",businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@authz.hasPerm('sensitive:add')")
    @Operation(summary = "新增敏感词", method = "POST")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated SensitiveAddQuery query) {
        sensitiveService.add(query);
        return ResponseResult.success();
    }

    @Log(title = "修改敏感词",businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@authz.hasPerm('sensitive:edit')")
    @Operation(summary = "修改敏感词", method = "POST")
    @PostMapping("/edit/{id}")
    public ResponseResult edit(@PathVariable("id") Long id, @RequestBody @Validated SensitiveAddQuery query) {
        query.setId(id);
        sensitiveService.update(query);
        return ResponseResult.success();
    }

    @Log(title = "删除敏感词",businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@authz.hasPerm('sensitive:delete')")
    @Operation(summary = "删除敏感词", method = "POST")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody SensitiveQuery query) {
        sensitiveService.delete(query);
        return ResponseResult.success();
    }

    @Log(title = "修改敏感词状态",businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@authz.hasPerm('sensitive:edit:status')")
    @Operation(summary = "修改敏感词状态", method = "POST")
    @PostMapping("/editStatus")
    public ResponseResult editStatus(@RequestBody SensitiveQuery query) {
        sensitiveService.editStatus(query);
        return ResponseResult.success();
    }

    @Log(title = "敏感词详情",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('sensitive:get')")
    @Operation(summary = "敏感词详情", method = "POST")
    @PostMapping("/get/{id}")
    public ResponseResult<SensitiveVo> get(@PathVariable("id") Long id, @RequestBody SensitiveQuery query) {
        query.setId(id);
        sensitiveService.get(query);
        return ResponseResult.success();
    }

    @Log(title = "敏感词列表",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('sensitive:list')")
    @Operation(summary = "敏感词列表", method = "POST")
    @PostMapping("/list")
    public ResponseResult<PageInfo<SensitiveVo>> list(@RequestBody SensitiveQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(),query.getSortField(),query.getSort());
        List<SensitiveVo> list = sensitiveService.getList(query);
        PageInfo<SensitiveVo> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    @Log(title = "单聊敏感词记录列表",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('sensitive:record:single:list')")
    @Operation(summary = "单聊敏感词记录列表", method = "POST")
    @PostMapping("/record/single/list")
    public ResponseResult<PageInfo<SensitiveSingleRecordVo>> singleRecordList(@RequestBody SensitiveRecordQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(),query.getSortField(),query.getSort());
        List<SensitiveSingleRecordVo> list = iSensitiveWordsResultService.singleRecordList(query);
        PageInfo<SensitiveSingleRecordVo> pageInfo = new PageInfo<>();
        return ResponseResult.success(pageInfo);
    }

    @Log(title = "群聊敏感词记录列表",businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('sensitive:record:group:list')")
    @Operation(summary = "群聊敏感词记录列表", method = "POST")
    @PostMapping("/record/group/list")
    public ResponseResult<PageInfo<SensitiveGroupRecordVo>> groupRecordList(@RequestBody SensitiveRecordQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(),query.getSortField(),query.getSort());
        List<SensitiveGroupRecordVo> list = iSensitiveWordsResultService.groupRecordList(query);
        PageInfo<SensitiveGroupRecordVo> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }
}
